package com.cart.models;

import com.cart.pojo.Cart;
import com.cart.pojo.CartItem;
import com.cart.pojo.OrderHistory;
import com.cart.pojo.Product;
import com.cart.services.OrderHistoryRepository;
import com.cart.services.ProductRepository;
import com.cart.services.UserRepository;
import com.cart.services.UserRepositoryImpl;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Scanner;

public class Customer extends User {

    private Cart cart;  // 每个客户有一个购物车

    public Cart getCart() {
        return cart;
    }

    public Customer(int id, String username, String password, String email, String phone, String userLevel, String registrationDate) {
        super(id, username, password, email, phone, userLevel, registrationDate, "1");
        this.cart = new Cart();
    }


    // 客户功能：将商品加入购物车
    public void addToCart(ProductRepository productRepository, int productId, int quantity) {
        Product product = productRepository.findById(productId);
        if (product != null && product.getQuantity() >= quantity) {
            cart.addProduct(product, quantity);
            System.out.println("商品已加入购物车");
        } else {
            System.out.println("库存不足或商品不存在");
        }
    }

    // 客户功能：从购物车中移除商品
    public void removeFromCart(int productId) {
        if (confirmRemoval()) {
            cart.removeProduct(productId);
        }
    }

    private boolean confirmRemoval() {
        System.out.println("确认移除商品吗？(y/n)");
        Scanner scanner = new Scanner(System.in);
        return scanner.nextLine().equalsIgnoreCase("y");
    }

    // 客户功能：修改购物车中的商品
    public void updateCart(int productId, int newQuantity) {
        if (newQuantity <= 0) {
            cart.removeProduct(productId);
        } else {
            cart.updateProductQuantity(productId, newQuantity);
        }
    }

    // 客户功能：结账
    public void checkout(ProductRepository productRepository) {
        List<CartItem> items = cart.getItems();
        for (CartItem item : items) {
            Product product = productRepository.findById(item.getProduct().getId());
            if (product != null && product.getQuantity() >= item.getQuantity()) {
                product.setQuantity(product.getQuantity() - item.getQuantity());
                productRepository.update(product);
            } else {
                System.out.println("库存不足或商品不存在");
            }
        }
        cart.clear();
        System.out.println("结账成功");
    }

    // 客户功能：查看购物历史
    public void viewOrderHistory(OrderHistoryRepository orderHistoryRepository) {
        List<OrderHistory> histories = orderHistoryRepository.findByUserId(this.getId());
        for (OrderHistory history : histories) {
            System.out.println(history.toString());
        }
    }

    // 清空购物车
    public void clearCart() {
        this.cart.clear();  // 调用购物车的 clear 方法
    }

    public void changePassword(String newPassword) throws InvocationTargetException {
        this.setPassword(newPassword);
        UserRepository userRepository = new UserRepositoryImpl();
        userRepository.save(this);
        System.out.println("密码修改成功！");
    }
}
